﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LTSMZ.life.Model;
using Qianli.DirSir.Common;
using System.Data.SqlClient;
using System.Data;

namespace LTSMZ.life.DAL
{
    public class PImgDal
    {
        SQLHelper sql = new SQLHelper();
        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public int Add(PImg p)
        {
            string str = "insert into PImg values('"+p.PImgHref+"',"+p.PImgid+","+p.Pzhu+")";
            return sql.ExecNonquery(str);
        }
        /// <summary>
        /// 显示图片
        /// </summary>
        /// <returns></returns>
        public List<PImg> ShowPImg()
        {
            string str = "select PImg.PImgHref,Product.Pname,PImg.Pid,PImgid from Product join PImg on PImg.Pid=Product.Pid";
            DataTable dt = sql.ExecDataTable(str);
            List<PImg> list = new List<PImg>();
            foreach (DataRow d in dt.Rows)
            {
                PImg p = new PImg();
                p.Pid = Convert.ToInt32(d["Pid"]);
                p.Pname = d["Pname"].ToString();
                p.PImgHref = d["PImgHref"].ToString();
                p.PImgid = Convert.ToInt32(d["PImgid"]);
                list.Add(p);
            }
            return list;
        }
        /// <summary>
        /// 根据商品编号获取商品图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<PImg> GetPimg(int id)
        {
            string str = "select ROW_NUMBER() over(order by Pzhu desc) ,* from PImg where Pid="+id+"";
            DataTable dt = sql.ExecDataTable(str);
            List<PImg> list = new List<PImg>();
            foreach (DataRow d in dt.Rows)
            {
                PImg p = new PImg();
                p.Pid = Convert.ToInt32(d["Pid"]);
                p.PImgHref = d["PImgHref"].ToString();
                p.PImgid = Convert.ToInt32(d["PImgid"]);
                list.Add(p);
            }
            return list;
        }
        /// <summary>
        /// 根据商品编号获取商品主图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<PImg> GetPimgzhu(int id)
        {
            string str = "select ROW_NUMBER() over(order by Pzhu desc) ,* from PImg where Pid=" + id + " and Pzhu=1";
            DataTable dt = sql.ExecDataTable(str);
            List<PImg> list = new List<PImg>();
            foreach (DataRow d in dt.Rows)
            {
                PImg p = new PImg();
                p.Pid = Convert.ToInt32(d["Pid"]);
                p.PImgHref = d["PImgHref"].ToString();
                p.PImgid = Convert.ToInt32(d["PImgid"]);
                list.Add(p);
            }
            return list;
        }
    }
}
